﻿using System;
using System.Linq;
using Biz.Buffer;
using Biz.EF.Platform.Kq;
using Hjt212Parser;
using log4net;

namespace Biz.Process
{
    /// <summary>
    /// 主要处理统计，日数据统计到月数据，月数据统计到年
    /// </summary>
    public static class ProcessorSummation
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof (ProcessorSummation));
        /// <summary>
        /// 根据[Envi_PS_Waste_Water_Flow_Hour_Correct_Data]数据统计年
        /// </summary>
        public static void Sum2Year()
        {
            try
            {
                var now = DateTime.Now;
                var begin = DateTimeHelper.YearBegin(now);
                var next = DateTimeHelper.NextYear(now);
                var yearStr = now.Year + "";
                using (var pCtx = new PlatformYgContext())
                {
                    var ports = CommonInformation.GetAllLetports();
                    foreach (var port in ports)
                    {
                        var hours = pCtx.Envi_PS_Waste_Water_Flow_Hour_Correct_Data
                            .Where(t => t.Date_Time >= begin)
                            .Where(t => t.Date_Time < next)
                            .ToList();
                        var years = pCtx.Envi_PS_Waste_Water_Flow_Year_Data
                            .Where(t => t.Let_Port_ID.Equals(port.ID + ""))
                            .Where(t => t.Pollution_Source_ID.Equals(port.Pollution_Source_Id + ""))
                            .Where(t => t.Year.Equals(yearStr)).ToList();
                        var gt1 = false;
                        if (years.Count > 0)
                        {
                            foreach (var year in years)
                            {
                                if (!gt1)
                                {
                                    try
                                    {
                                        //year.Pollution_Source_ID = port.Pollution_Source_Id + "";
                                        //year.Let_Port_ID = port.ID + "";
                                        year.Year_Flow = hours.Sum(t => t.Flow);
                                        year.AVG_COD = hours.Average(t => t.AVG_COD);
                                        year.COD_Year_Measure = year.Year_Flow * year.AVG_COD / 1000;
                                        year.AVG_AN = hours.Average(t => t.AVG_AN);
                                        year.AN_Year_Measure = year.Year_Flow * year.AVG_AN / 1000;
                                    }
                                    catch (Exception ex)
                                    {
                                        Log.Error(ex);
                                    }
                                }
                                else
                                {
                                    pCtx.Envi_PS_Waste_Water_Flow_Year_Data.Remove(year);
                                }
                                gt1 = true;
                            }
                        }
                        else
                        {
                            try
                            {
                                var year = new Envi_PS_Waste_Water_Flow_Year_Data();
                                year.Year = yearStr;
                                year.Pollution_Source_ID = port.Pollution_Source_Id + "";
                                year.Let_Port_ID = port.ID + "";
                                year.Year_Flow = hours.Sum(t => t.Flow);
                                year.AVG_COD = hours.Average(t => t.AVG_COD);
                                year.COD_Year_Measure = year.Year_Flow * year.AVG_COD / 1000;
                                year.AVG_AN = hours.Average(t => t.AVG_AN);
                                year.AN_Year_Measure = year.Year_Flow * year.AVG_AN / 1000;
                                pCtx.Envi_PS_Waste_Water_Flow_Year_Data.Add(year);
                            }
                            catch (Exception ex)
                            {
                                Log.Error(ex);
                            }
                        }
                    }
                    pCtx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        /// <summary>
        /// 根据[Envi_PS_Waste_Water_Flow_Hour_Correct_Data]数据统计季
        /// </summary>
        public static void Sum2Quarter()
        {
            try
            {
                var now = DateTime.Now;
                var begin = DateTimeHelper.QuarterBegin(now);
                var next = DateTimeHelper.NextQuarter(now);
                var quarterStr = now.Year + "-" + DateTimeHelper.Quarter(now);
                using (var pCtx = new PlatformYgContext())
                {
                    var ports = CommonInformation.GetAllLetports();
                    foreach (var port in ports)
                    {
                        var hours = pCtx.Envi_PS_Waste_Water_Flow_Hour_Correct_Data
                            .Where(t => t.Date_Time >= begin)
                            .Where(t => t.Date_Time < next)
                            .ToList();
                        var quarters = pCtx.Envi_PS_Waste_Water_Flow_Quarter_Data
                            .Where(t => t.Let_Port_ID.Equals(port.ID + ""))
                            .Where(t => t.Pollution_Source_ID.Equals(port.Pollution_Source_Id + ""))
                            .Where(t => t.Quarter.Equals(quarterStr)).ToList();
                        var gt1 = false;
                        if (quarters.Count > 0)
                        {
                            foreach (var quarter in quarters)
                            {
                                if (!gt1)
                                {
                                    try
                                    {
                                        //year.Pollution_Source_ID = port.Pollution_Source_Id + "";
                                        //year.Let_Port_ID = port.ID + "";
                                        quarter.Quarter_Flow = hours.Sum(t => t.Flow);
                                        quarter.AVG_COD = hours.Average(t => t.AVG_COD);
                                        quarter.COD_Quarter_Measure = quarter.Quarter_Flow * quarter.AVG_COD / 1000;
                                        quarter.AVG_AN = hours.Average(t => t.AVG_AN);
                                        quarter.AN_Quarter_Measure = quarter.Quarter_Flow * quarter.AVG_AN / 1000;
                                    }
                                    catch (Exception ex)
                                    {
                                        Log.Error(ex);
                                    }
                                }
                                else
                                {
                                    pCtx.Envi_PS_Waste_Water_Flow_Quarter_Data.Remove(quarter);
                                }
                                gt1 = true;
                            }
                        }
                        else
                        {
                            try
                            {
                                var quarter = new Envi_PS_Waste_Water_Flow_Quarter_Data();
                                quarter.Quarter = quarterStr;
                                quarter.Pollution_Source_ID = port.Pollution_Source_Id + "";
                                quarter.Let_Port_ID = port.ID + "";
                                quarter.Quarter_Flow = hours.Sum(t => t.Flow);
                                quarter.AVG_COD = hours.Average(t => t.AVG_COD);
                                quarter.COD_Quarter_Measure = quarter.Quarter_Flow * quarter.AVG_COD / 1000;
                                quarter.AVG_AN = hours.Average(t => t.AVG_AN);
                                quarter.AN_Quarter_Measure = quarter.Quarter_Flow * quarter.AVG_AN / 1000;
                                pCtx.Envi_PS_Waste_Water_Flow_Quarter_Data.Add(quarter);
                            }
                            catch (Exception ex)
                            {
                                Log.Error(ex);
                            }
                        }
                    }
                    pCtx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        /// <summary>
        /// 根据[Envi_PS_Waste_Water_Flow_Hour_Correct_Data]数据统计月
        /// </summary>
        public static void Sum2Month()
        {
            try
            {
                var now = DateTime.Now;
                var begin = DateTimeHelper.MonthBegin(now);
                var next = DateTimeHelper.NextMonth(now);
                var monthStr = begin.ToString("yyyy-MM");
                using (var pCtx = new PlatformYgContext())
                {
                    var ports = CommonInformation.GetAllLetports();
                    foreach (var port in ports)
                    {
                        var hours = pCtx.Envi_PS_Waste_Water_Flow_Hour_Correct_Data
                            .Where(t => t.Date_Time >= begin)
                            .Where(t => t.Date_Time < next)
                            .ToList();
                        var months = pCtx.Envi_PS_Waste_Water_Flow_Month_Data
                            .Where(t => t.Let_Port_ID.Equals(port.ID + ""))
                            .Where(t => t.Pollution_Source_ID.Equals(port.Pollution_Source_Id + ""))
                            .Where(t => t.Month.Equals(monthStr)).ToList();
                        var gt1 = false;
                        if (months.Count > 0)
                        {
                            foreach (var month in months)
                            {
                                if (!gt1)
                                {
                                    try
                                    {
                                        //year.Pollution_Source_ID = port.Pollution_Source_Id + "";
                                        //year.Let_Port_ID = port.ID + "";
                                        month.Month_Flow = hours.Sum(t => t.Flow);
                                        month.AVG_COD = hours.Average(t => t.AVG_COD);
                                        month.COD_Month_Measure = month.Month_Flow * month.AVG_COD / 1000;
                                        month.AVG_AN = hours.Average(t => t.AVG_AN);
                                        month.AN_Month_Measure = month.Month_Flow * month.AVG_AN / 1000;
                                    }
                                    catch (Exception ex)
                                    {
                                        Log.Error(ex);
                                    }
                                }
                                else
                                {
                                    pCtx.Envi_PS_Waste_Water_Flow_Month_Data.Remove(month);
                                }
                                gt1 = true;
                            }
                        }
                        else
                        {
                            try
                            {
                                var month = new Envi_PS_Waste_Water_Flow_Month_Data();
                                month.Month = monthStr;
                                month.Pollution_Source_ID = port.Pollution_Source_Id + "";
                                month.Let_Port_ID = port.ID + "";
                                month.Month_Flow = hours.Sum(t => t.Flow);
                                month.AVG_COD = hours.Average(t => t.AVG_COD);
                                month.COD_Month_Measure = month.Month_Flow * month.AVG_COD / 1000;
                                month.AVG_AN = hours.Average(t => t.AVG_AN);
                                month.AN_Month_Measure = month.Month_Flow * month.AVG_AN / 1000;
                                pCtx.Envi_PS_Waste_Water_Flow_Month_Data.Add(month);
                            }
                            catch (Exception ex)
                            {
                                Log.Error(ex);
                            }
                        }
                    }
                    pCtx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
    }
}
